package com.startery.distributedLock;

import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.startery.ConnectionFactory;

public class OptimisticLock {
public static void main(String[] args) throws InterruptedException {
	RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();

    //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
    redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

    //使用StringRedisSerializer来序列化和反序列化redis的ke
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());

    //开启事务
    redisTemplate.setEnableTransactionSupport(true);
    redisTemplate.setConnectionFactory(ConnectionFactory.getInstance());
    redisTemplate.afterPropertiesSet();
    BoundValueOperations<Object,Object> aa=redisTemplate.boundValueOps("aa");
    BoundValueOperations<Object,Object> bb=redisTemplate.boundValueOps("bb");
    aa.set("aa0");
    System.out.println(aa.get());//aa0
    redisTemplate.watch("aa");
    redisTemplate.multi();
    aa.set("aa1");
    System.out.println(aa.get());//null 还没有执行exec拿不到值
    bb.set("bb1");
    Thread.sleep(30000l);
    redisTemplate.exec();
    System.out.println(aa.get());//打印客户端b在30s种设置的12313
    System.out.println(bb.get());//打印bb1
}
}
